home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Disc to the Future 2
/
Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin
/
MAC
/
MPW_TOOL
/
TOOLS
/
TOOLS_WI
/
ICON_8
/
ICONT_FO
/
TRANS.C
< prev
next >
Wrap
Text File
|
1990-03-02
|
4KB
|
169 lines
/*
* trans.c - main control of the translation process.
*/
#include "::h:config.h"
#include "tproto.h"
#include "::h:version.h"
#include "globals.h"
#include "trans.h"
#include "general.h"
#include "tsym.h"
#include "tree.h"
#include "token.h"
/*
* Prototypes.
*/
FILE *preprocess Params((char *filename));
novalue trans1 Params((char *filename));
char *comfile = NULL;
int tfatals; /* total number of fatal errors */
int nocode; /* non-zero to suppress code generation */
int in_line; /* current input line number */
int incol; /* current input column number */
int peekc; /* one-character look ahead */
FILE *srcfile; /* current input file */
FILE *codefile; /* current ucode output file */
FILE *globfile; /* current global table output file */
/*
* translate a number of files, returning an error count
*/
int trans(ifiles)
char **ifiles;
{
tmalloc(); /* allocate memory for translation */
#ifdef MultipleRuns
yylexinit(); /* initialize lexical analyser */
tcodeinit(); /* initialize code generator */
#endif /* Multiple Runs */
while (*ifiles)
trans1(*ifiles++); /* translate each file in turn */
tmfree(); /* free memory used for translation */
/*
* Report information about errors and warnings and be correct about it.
*/
if (tfatals == 1)
fprintf(stderr, "1 error\n");
else if (tfatals > 1)
fprintf(stderr, "%d errors\n", tfatals);
else if (!silent)
fprintf(stderr, "No errors\n");
return tfatals;
}
/*
* translate one file.
*/
static novalue trans1(filename)
char *filename;
{
char oname[MaxFileName]; /* buffer for constructing file names */
comfile = filename;
tfatals = 0; /* reset error counts */
nocode = 0; /* allow code generation/*
in_line = 1; /* start with line 1, column 0 */
incol = 0;
peekc = 0; /* clear character lookahead */
if (m4pre)
srcfile = preprocess(filename);
else if (strcmp(filename,"-") == 0) {
srcfile = stdin;
filename = "stdin";
}
else
srcfile = fopen(filename,"r");
if (srcfile == NULL)
quitf("cannot open %s",filename);
if (!silent)
fprintf(stderr, "%s:\n",filename);
#ifndef VarTran
/*
* Form names for the .u1 and .u2 files and open them.
* Write the ucode version number to the .u2 file.
*/
makename(oname, TargetDir, filename, U1Suffix);
codefile = fopen(oname, "w");
if (codefile == NULL)
quitf("cannot create %s", oname);
makename(oname, TargetDir, filename, U2Suffix);
globfile = fopen(oname, "w");
if (globfile == NULL)
quitf("cannot create %s", oname);
writecheck(fprintf(globfile,"version\t%s\n",UVersion));
#endif /* VarTran */
tok_loc.n_file = filename;
in_line = 1;
tminit(); /* Initialize data structures */
yyparse(); /* Parse the input */
/*
* Close the output files and the input file.
*/
#ifndef VarTran
if (fclose(codefile) != 0 || fclose(globfile) != 0)
quit("cannot close ucode file");
#endif /* VarTran */
if (!m4pre)
fclose(srcfile);
/* "else" is below in conditional */
#if UNIX
else if (pclose(srcfile) != 0)
quit("m4 terminated abnormally");
#endif /* UNIX */
}
/*
* writecheck - check the return code from a stdio output operation
*/
novalue writecheck(rc)
{
if (rc < 0)
quit("cannot write to ucode file");
}
/*
* open a pipe to the preprocessor.
*/
static FILE *preprocess(filename)
char *filename;
{
#if MACINTOSH
#if MPW
/* #pragma unused(filename) */
return NULL; /* to prevent compiler warning */
#endif /* MPW */
#endif /* MACINTOSH */
#if UNIX
{
FILE *f, *popen();
char *s = alloc((unsigned int)(4+strlen(filename)));
sprintf(s,"m4 %s",filename);
f = popen(s,"r");
free(s);
return f;
}
#endif /* UNIX */
}